const domainName = getApp().globalData.domainName
const parameter = getApp().globalData.parameter

// pages/sysConfig/Test_types/Test_types.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lis:[],
    hide_list:[],
    hide_list1:[],
    hide_list2:[],
    show_list3:[],
  },

  set_test_types(e){
    let index = e.detail.value
    let test_types_id = this.data.test_types[index].id
    let test_types_name = this.data.test_types[index].name
    this.setData({
      test_types_id,
      test_types_name,
    })
  },

  set_point(e){
    getApp().get_vibration()
    let point_id = e.currentTarget.id
    let point_name = e.currentTarget.dataset.name
    let description = e.currentTarget.dataset.description
    
    wx.showActionSheet({
      itemList: ['修改要点','删除要点'],
      alertText: '要点设置',
      success: res => {
        if(res.tapIndex==0){
          let url_data = {
            point_id:point_id,
            name:point_name,
            description:description,
          }
          wx.navigateTo({
            url: '/pages/sysConfig/Test_points_edit/Test_points_edit?url_data=' + encodeURIComponent(JSON.stringify(url_data))
          })
          this.can_onshow = true
        }else if(res.tapIndex==1){
          wx.showModal({
            content: '确定要删除【'+point_name+'】要点及其描述吗？',
            title: '警告',
            success: res => {
              if(res.confirm){
                wx.request({
                  url: domainName + 'Test_subjects_delete_point',
                  data: {point_id},...parameter,
                  success: res => {
                    if(res.data.update_res){
                      wx.showToast({
                        title: '删除完成',
                      })
                      this.page_init()
                    }
                  },
                })
              }
            },
          })

        }
      },
    })
  },

  set_content(e){
    getApp().get_vibration()
    let content_id = e.currentTarget.id
    let name = e.currentTarget.dataset.name
    wx.showActionSheet({
      itemList: ['修改内容（本级操作）','删除内容（本级操作）','要点管理（下级操作）'],
      alertText: '内容设置',
      success: res => {
        if(res.tapIndex==0){
          wx.navigateTo({
            url: '/pages/sysConfig/Test_contents_edit/Test_contents_edit?content_id=' + content_id + '&name=' + name,
          })
          this.can_onshow = true
        }else if(res.tapIndex==1){
          wx.showModal({
            content: '确定删除吗？',
            title: '提示',
            success: res => {
              if(res.confirm){
                wx.request({
                  url: domainName + 'Test_subjects_delete_content',
                  data: {content_id},...parameter,
                  success: res => {
                    if(res.data.update_res){
                      wx.showToast({
                        title: '删除完成',
                      })
                      this.page_init()
                    }
                  },
                })
              }
            },
          })
        }else if(res.tapIndex==2){
          wx.navigateTo({
            url: '/pages/sysConfig/Test_points/Test_points?content_id=' + content_id + '&name=' + name,
          })
          this.can_onshow = true
        }
      },
    })
  },

  set_stage(e){
    getApp().get_vibration()
    let stage_id = e.currentTarget.dataset.id
    let stage_name = e.currentTarget.dataset.name
    wx.showActionSheet({
      itemList: ['修改阶段名称（本级操作）','删除阶段（本级操作）','训练内容管理（下级操作）'],
      alertText: '阶段设置',
      success: res=> {
        if(res.tapIndex==0){
          wx.navigateTo({
            url: '/pages/sysConfig/Test_stages_edit/Test_stages_edit?stage_id=' + stage_id + '&stage_name=' + stage_name,
          })
          this.can_onshow = true
        }else if(res.tapIndex==2){
          wx.navigateTo({
            url: '/pages/sysConfig/Test_contents/Test_contents?test_stages_id=' + stage_id + '&test_stages_name=' + stage_name,
          })
          this.can_onshow = true
        }else if(res.tapIndex==1){
          wx.request({
            url: domainName + 'Test_subjects_delete_stage',
            data: {stage_id},...parameter,
            success: res => {
              if(res.data.update_res){
                wx.showToast({
                  title: '删除完成！',
                })
                this.page_init()
              }
            },
          })
        }
      },
    })
  },

  set_subject(e){
    let index = e.currentTarget.dataset.index
    let test_subjects_id = this.data.lis[index].id
    let test_subjects_name = this.data.lis[index].name
    wx.showActionSheet({
      itemList: ['修改科目名称（本级操作）','训练阶段管理（下级操作）','删除该科目'],
      alertText: '设置科目',
      success: res => {
        if(res.tapIndex==0){
          wx.navigateTo({
            url: '/pages/sysConfig/Test_subjects_edit/Test_subjects_edit?test_subjects_id=' + test_subjects_id + '&test_subjects_name=' + test_subjects_name,
          })
          this.can_onshow = true

        }else if(res.tapIndex==1){
          wx.navigateTo({
            url: '/pages/sysConfig/Test_stages/Test_stages?test_subjects_id=' + test_subjects_id + '&test_subjects_name=' + test_subjects_name,
          })
          this.can_onshow = true
        }else if(res.tapIndex==2){
          wx.showModal({
            content: '确定要删除科目【'+ test_subjects_name + '】吗？（删除其包含的阶段、内容、要点、描述）',
            title: '提醒！',
            success: res => {
              if(res.confirm){
                wx.request({
                  url: domainName + 'Test_subjects_delete_subject',
                  data: {test_subjects_id},...parameter,
                  success: res => {
                    if(res.data.delete_res){
                      wx.showToast({
                        title: '删除完成！',
                      })
                    }
                  },
                })
              }
            },
          })
        }
      },
    })
  },



  hide(e){
    getApp().get_vibration()
    let id = e.currentTarget.id
    let hide_list = this.data.hide_list
    let list_index = hide_list.indexOf(id)
    if(list_index>-1){
      hide_list.splice(list_index, 1)
    }else{
      hide_list.push(id)
    }
    this.setData({
      hide_list,
    })
  },

  show3(e){
    getApp().get_vibration()
    let id = e.currentTarget.id
    let show_list3 = this.data.show_list3
    let list_index = show_list3.indexOf(id)
    if(list_index>-1){
      show_list3.splice(list_index, 1)
    }else{
      show_list3.push(id)
    }
    this.setData({
      show_list3,
    })
  },

  hide2(e){
    getApp().get_vibration()
    let id = e.currentTarget.id
    let hide_list2 = this.data.hide_list2
    let list_index = hide_list2.indexOf(id)
    if(list_index>-1){
      hide_list2.splice(list_index, 1)
    }else{
      hide_list2.push(id)
    }
    this.setData({
      hide_list2,
    })
  },

  hide1(e){
    getApp().get_vibration()
    let id = e.currentTarget.id
    let hide_list1 = this.data.hide_list1
    let list_index = hide_list1.indexOf(id)
    if(list_index>-1){
      hide_list1.splice(list_index, 1)
    }else{
      hide_list1.push(id)
    }
    this.setData({
      hide_list1,
    })
  },


  confirm_add(){
    getApp().get_vibration()
    wx.showModal({
      content: '确定提交吗？',
      title: '提示',
      success: res => {
        if(res.confirm){
          wx.request({
            url: domainName + 'Test_subjects_submit',
            data: {
              name:this.data.name,
              test_types_id:this.data.test_types_id,
            },...parameter,
            success: res => {
              if(res.data.row_id){
                wx.showToast({
                  title: '提交完成！',
                })
                this.setData({show:false})
                this.page_init()
              }
            },
          })
        }
      },
    })
  },

  input_name(e){
    this.setData({
      name:e.detail.value
    })
  },

  add(){
    getApp().get_vibration()
    this.setData({show:true})
  },

  page_init(){
    wx.request({
      url: domainName + 'Test_subjects_init',
      data: {},...parameter,
      success: res => {
        this.setData({
          lis:res.data.lis,
          test_types:res.data.test_types,
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.page_init()
  },

  onShow(){
    if(this.can_onshow){
      this.page_init()
      this.can_onshow = false
    }
  },

})